﻿#include "EquipmentMaintaince.h"
#include "ui_equipmentmaintaince.h"

EquipmentMaintaince::EquipmentMaintaince(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::EquipmentMaintaince)
{
    ui->setupUi(this);
    time = new QLabel(ui->frameBottom);
    time->setGeometry(10, 10, 200, 40);
    time->setStyleSheet("QLabel{font: 20px; color: rgb(25, 25, 112);}");
    signal = new QLabel(ui->frameBottom);
    signal->setGeometry(700, 18, 41, 22);
    signal->setStyleSheet("QLabel{background-color:transparent;}");

    QDir dir(IMAGE_PATH);
    if(dir.exists())
    {
        ui->frameTop->setStyleSheet(LOGO_STY);
    }

    QTimer *UnlockEnable = new QTimer(this);
    connect(UnlockEnable,SIGNAL(timeout()), this, SLOT(UnlockEnableSlot()));
//    UnlockEnable->start(500);
    FaultInfo.setParent(ui->frameMid);
    FaultInfo.move(150, 270);
    FaultInfo.resize(400, 30);
    FaultInfo.setStyleSheet("QLabel{font-size:20px; color: red;}");
//    FaultInfo.mysetText(tr("所有东西都故障了"));
    ui->labelErrorMeter->hide();
    ui->label_2->hide();
    ui->labelErrorCard->hide();
    F5_Timer = new QTimer(this);
    connect(F5_Timer, SIGNAL(timeout()), this, SLOT(Show_ErrorInfomation()));
    connect(&this->timerLongPress, SIGNAL(timeout()), this, SLOT(onClickedLongCount()));
    connect(stackLayout, SIGNAL(currentChanged(int)), this, SLOT(F5_ErrorInfo_Timer_go(int)));
#if (GUN_AMOUNT == 1)
    ui->pushButton->hide();
#endif
}

EquipmentMaintaince::~EquipmentMaintaince()
{
    delete ui;
}
void EquipmentMaintaince::F5_ErrorInfo_Timer_go(int current)
{
    if(current == equipmentMaintaincePage)
    {
        ui->_focus_gun_->setText(QString::number(focus_gun_));
        F5_Timer->start(1000);
        FaultInfo.mysetText(ErrorString[focus_gun_-1]);
    }
    else
    {
        F5_Timer->stop();
    }
}

void EquipmentMaintaince::Show_ErrorInfomation()
{
    ErrorMeter();
    ErrorCAN();
    ErrorCard();
    ErrorHardware();
}
void EquipmentMaintaince::ErrorHardware()//遥信故障信号
{
    if(chargingData->gunmsg[focus_gun_-1].errcode[0] != 0)
    {
        for(int i=0; i<11; i++)
        {
            if(Error[focus_gun_-1][i] != chargingData->gunmsg[focus_gun_-1].errcode[i])
            {
//                if(chargingData->gunmsg[focus_gun_-1].errcode[0] > 10)
//                    chargingData->gunmsg[focus_gun_-1].errcode[0] = 10;
                for(int i=0; i<11; i++)
                    Error[focus_gun_-1][i] = chargingData->gunmsg[focus_gun_-1].errcode[i];
                loadingagainInfo();
                break;
            }
        }
    }
    else
    {
        FaultInfo.myClear();
    }
}
void EquipmentMaintaince::loadingagainInfo()
{
    ErrorString[focus_gun_-1].clear();
    for(int i=0; i<Error[focus_gun_-1][0]; i++)
    {
        switch(Error[focus_gun_-1][i+1])
        {
        case 1: ErrorString[focus_gun_-1] += tr("其他故障\n"); break;
        case 4: ErrorString[focus_gun_-1] += tr("急停按钮动作故障\n"); break;
        case 5: ErrorString[focus_gun_-1] += tr("烟雾传感器感故障\n"); break;
        case 6: ErrorString[focus_gun_-1] += tr("交流输入断路器故障\n"); break;
        case 7: ErrorString[focus_gun_-1] += tr("直流母线输出接触器拒动/误动故障\n"); break;
        case 8: ErrorString[focus_gun_-1] += tr("直流母线输出熔断器故障\n"); break;
        case 9: ErrorString[focus_gun_-1] += tr("充电接口电子锁故障\n"); break;
        case 10: ErrorString[focus_gun_-1] += tr("充电桩风扇故障\n"); break;
        case 11: ErrorString[focus_gun_-1] += tr("避雷器故障\n"); break;
        case 12: ErrorString[focus_gun_-1] += tr("绝缘监测故障\n"); break;
        case 13: ErrorString[focus_gun_-1] += tr("电池极性反接故障\n"); break;
        case 14: ErrorString[focus_gun_-1] += tr("充电中车辆控制导引故障\n"); break;
        case 15: ErrorString[focus_gun_-1] += tr("充电桩过温故障\n"); break;
        case 16: ErrorString[focus_gun_-1] += tr("充电接口过温故障\n"); break;
        case 17: ErrorString[focus_gun_-1] += tr("充电枪未归位告警\n"); break;
        case 18: ErrorString[focus_gun_-1] += tr("BMS通信故障\n"); break;
        case 19: ErrorString[focus_gun_-1] += tr("输入电压过压故障\n"); break;
        case 20: ErrorString[focus_gun_-1] += tr("输入电压欠压故障\n"); break;
        case 21: ErrorString[focus_gun_-1] += tr("直流母线输出过压故障\n"); break;
        case 22: ErrorString[focus_gun_-1] += tr("直流母线输出欠压故障\n"); break;
        case 23: ErrorString[focus_gun_-1] += tr("直流母线输出过流故障\n"); break;
        case 24: ErrorString[focus_gun_-1] += tr("充电模块故障\n"); break;
        case 25: ErrorString[focus_gun_-1] += tr("充电模块交流输入故障\n"); break;
        case 26: ErrorString[focus_gun_-1] += tr("充电模块交流输入过压故障\n"); break;
        case 27: ErrorString[focus_gun_-1] += tr("充电模块交流输入欠压故障\n"); break;
        case 28: ErrorString[focus_gun_-1] += tr("充电模块交流输入缺相故障\n"); break;
        case 29: ErrorString[focus_gun_-1] += tr("充电模块直流输出短路故障\n"); break;
        case 30: ErrorString[focus_gun_-1] += tr("充电模块直流输出过流故障\n"); break;
        case 31: ErrorString[focus_gun_-1] += tr("充电模块直流输出过压故障\n"); break;
        case 32: ErrorString[focus_gun_-1] += tr("充电模块直流输出欠压故障\n"); break;
        case 33: ErrorString[focus_gun_-1] += tr("充电模块过温故障\n"); break;
        case 34: ErrorString[focus_gun_-1] += tr("充电模块通信故障\n"); break;
        case 35: ErrorString[focus_gun_-1] += tr("充电模块风扇故障\n"); break;
        case 42: ErrorString[focus_gun_-1] += tr("门禁故障\n"); break;
        case 43: ErrorString[focus_gun_-1] += tr("直流输出接触器粘连故障\n"); break;
        case 44: ErrorString[focus_gun_-1] += tr("绝缘监测告警\n"); break;
        case 45: ErrorString[focus_gun_-1] += tr("泄放回路故障\n"); break;
        case 46: ErrorString[focus_gun_-1] += tr("充电桩过温告警\n"); break;
        case 47: ErrorString[focus_gun_-1] += tr("充电枪过温告警\n"); break;
        case 48: ErrorString[focus_gun_-1] += tr("交流输入接触器据动/误动故障\n"); break;
        case 49: ErrorString[focus_gun_-1] += tr("交流输入接触器粘连故障\n"); break;
        case 50: ErrorString[focus_gun_-1] += tr("辅助电源故障\n"); break;
        case 51: ErrorString[focus_gun_-1] += tr("并联接触器据动/误动故障\n"); break;
        case 52: ErrorString[focus_gun_-1] += tr("并联接触器粘连故障\n"); break;
        case 59: ErrorString[focus_gun_-1] += tr("功率模块通信故障\n"); break;
        case 61: ErrorString[focus_gun_-1] += tr("功率模块故障\n"); break;
        case 63: ErrorString[focus_gun_-1] += tr("开关模块通信故障\n"); break;
        case 65: ErrorString[focus_gun_-1] += tr("开关模块故障\n"); break;
        case 67: ErrorString[focus_gun_-1] += tr("直流充电主控模块判断BMS故障\n"); break;
        case 69: ErrorString[focus_gun_-1] += tr("充电模块泄放故障\n"); break;
        case 70: ErrorString[focus_gun_-1] += tr("绝缘监测仪通信故障\n"); break;
        case 71: ErrorString[focus_gun_-1] += tr("环境监控板通信故障\n"); break;
        }
        FaultInfo.mysetText(ErrorString[focus_gun_-1]);
    }
}
void EquipmentMaintaince::ErrorMeter()
{
    if(chargingData->gunmsg[focus_gun_-1].meter_state > 0)//电表故障
    {
        ui->labelErrorMeter->show();
    }
    else
    {
        ui->labelErrorMeter->hide();
    }
}

void EquipmentMaintaince::ErrorCAN()
{
    if(chargingData->can_state[focus_gun_-1] > 0)//CAN故障
    {
        ui->label_2->show();
    }
    else
    {
        ui->label_2->hide();
    }

}

void EquipmentMaintaince::ErrorCard()
{
    if(card->errcode > 0)//读卡器故障
    {
        ui->labelErrorCard->show();
    }
    else
    {
        ui->labelErrorCard->hide();
    }
}
void EquipmentMaintaince::onClickedLongCount()
{
    timerLongPress.stop();
    /*待完成*/
    focus_gun_ = 0;
    stackLayout->setCurrentIndex(deviceMaintaiceforLogingPage);
}
void EquipmentMaintaince::on_buttonLogo_pressed()
{
        timerLongPress.start(3000);
}

void EquipmentMaintaince::on_buttonLogo_released()
{

    timerLongPress.stop();
}

void EquipmentMaintaince::on_pushButton_clicked()
{
    Init_Focus();
}
void EquipmentMaintaince::UnlockEnableSlot()
{
    if(chargingData->cmd5[focus_gun_-1].req_state != 0){
        ui->buttonUnlock->setEnabled(false);
        ui->buttonUnlock->setStyleSheet("QPushButton{background-color: darkGray;color: rgb(25, 25, 112); font: 18px; font-weight: bold;  border-radius: 10px;}");
    }else{
        ui->buttonUnlock->setEnabled(true);
        ui->buttonUnlock->setStyleSheet("QPushButton{background-color: rgb(126, 205, 244);color: rgb(25, 25, 112); font: 18px; font-weight: bold;  border-radius: 10px;} QPushButton:pressed{background-color: qlineargradient(x1:0,x2:0,y1:0,y2:1,stop:0 rgb(126, 205, 244), stop:0.8 rgb(126, 205, 244), stop: 1 rgb(153, 153, 153)); color: rgb(25, 25, 112); font: 20px; border-radius: 10px;}");
    }
}

void EquipmentMaintaince::on_buttonUnlock_clicked()
{
    chargingData->cmd5[focus_gun_-1].req_state = 1;
    chargingData->cmd5[focus_gun_-1].guntab = focus_gun_;
    chargingData->cmd5[focus_gun_-1].ctrltype = 100;
}
